home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 443 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  69 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: mixing enums and inte
  5. X-Nntp-Posting-Host: golden.ripco.com
  6. Message-ID: <DKpKrF.A2M@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Fri, 5 Jan 1996 12:45:15 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. glynis@yrkpa.kias.com (John Flinchbaugh) in <4cij6k$dvb@yrkpa.kias.com>
  13. asks:
  14.  
  15. >the faq states that mixing enums and ints is legal, but poor style.
  16.  
  17. The main problem is that an enumeration constant is only guaranteed to
  18. be an integer.  Note that "integer" does not mean an int.  It could be
  19. any of char, short, int, or long.  The standard specifies that
  20. "implementation-defined behavior" includes "The integer type chosen to
  21. represent the values of an enumeration type."
  22.  
  23. Any where mixing an int with a short, char, or long could cause a problem, it is
  24. conceivable that a problem could arise mixing ints and enums.
  25.  
  26. Another stylistic point is that you may want to rigorously treat enums
  27. as distinct from another type.  In fact, each distinct enumeration
  28. _does_ represent a different enumerated type.
  29.  
  30. >i'd like to use enums to organize my constants, like this:
  31.  
  32. >        enum DIRECT {UP=72,LEFT=75,RIGHT=77,DOWN=80};
  33. >        int key;
  34. >        getch();        /* dos code to get the second byte of 2-byte */
  35. >        key=getch();    /* key codes from keyboard                   */
  36. >        if (key==UP) printf("up");
  37. >        ...
  38.  
  39. >now this should work, but it's poor style, right?
  40.  
  41. Since you use the non-standard function getch(), you can't be concerned
  42. with portability.  Since, for implementation-defined behavior, "Each
  43. implementation shall document its behavior...", you can check your
  44. documentation to be sure that enums and ints mix without a problem.
  45. Just don't expect any implementation-defined behavior (including
  46. non-standard functions) to be portable, even to the next version of your
  47. compiler.
  48.  
  49. >would casting it be more proper:
  50.  
  51. >        if ((enum DIRECT) key==up) ...
  52.                                 ^^
  53.                            You meant UP, didn't you?
  54.  
  55. >also, can you typedef enums?  if so, how?
  56.  
  57. typedef enum {UP=72, LEFT=75, RIGHT=77, DOWN=80} DIRECT;
  58.  
  59. DIRECT which_way, last_way;
  60. /* ... */
  61.     last_way = which_way;
  62.     which_way = UP;
  63.     if (last_way == which_way) { /* foo */ }
  64.  
  65.                                             
  66. --
  67. * Martin Ambuhl       net: mambuhl@ripco.com
  68. * Chicago, IL (USA)    
  69.